package com.kiko.education.controller;

import cn.hutool.json.JSON;
import com.kiko.education.mapper.ClassInfoMapper;
import com.kiko.education.mapper.ClassMapper;
import com.kiko.education.mapper.CourseScoreMapper;
import com.kiko.education.model.CourseScore;
import com.kiko.education.model.User;
import com.kiko.education.service.CourseService;
import com.kiko.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class TeacherController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;
    @Autowired
    private CourseScoreMapper courseScoreMapper;
    @Autowired
    private CourseService courseService;

    @RequestMapping(value="/BInfo1", method = RequestMethod.POST)
    public String toStudentMange(String userID, Model model){
        User user = userService.getByUserID(userID);
        if(user != null){
            int classID = classMapper.getClassIDByID(user.getID());
            List<Integer> list = classInfoMapper.getStudentIDByID(classID);

            List<String> usersID =new ArrayList<>();
            List<String> usersName =new ArrayList<>();
            List<String> usersSex =new ArrayList<>();
            for(int i=0;i<list.size();i++){
                User user1 = userService.getByID(list.get(i));
                usersID.add(user1.getUserID());
                usersName.add(user1.getUserName());
                usersSex.add(user1.getSex());
            }
            model.addAttribute("usersID", usersID);
            model.addAttribute("usersName", usersName);
            model.addAttribute("usersSex", usersSex);
        }
        return "/BInfo";
    }

    @RequestMapping("/BInfo2")
    public String toScoreMange(List<String> usersID, Model model){
        List<String> courseNames = new ArrayList<>();
        List<Double> score = new ArrayList<>();
        for(int i=0;i<usersID.size();i++){
            List<CourseScore> scores = courseScoreMapper.getScoreByStuID(usersID.get(i));
            for(int j=0;j<scores.size();j++){
                int scoreID = scores.get(j).getCourseID();
                courseNames.add(courseService.getCourseNameByID(scoreID));
                score.add(scores.get(j).getScore());
            }
        }
        model.addAttribute("courseNames", courseNames);
        model.addAttribute("scores", score);
        return "/BInfo";
    }
}
